import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,Button  } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import Footer from '../index/footer'

import cat01 from '../../assets/kedouimg01/pcat02.png'
import cat02 from '../../assets/kedouimg01/nav07.png'
import cat03 from '../../assets/kedouimg01/sckf.png'
import pcat04 from '../../assets/kedouimg01/pcat04.png'

export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      result:[],
      tjinfo:'',
      pageNumber:0,
      hasNextPage:false,
      scrollPd:false,
      pageheight:0,
	  znwns:'',
	  appmark:CommonJs.getmark(),
    }
  }
  config = {
    navigationBarTitleText:'加载中'
  }
  dobt(){
	if(this.state.appmark=='xjzb'){
		Taro.setNavigationBarTitle({title:'共享仓'})
	}
	else{
		Taro.setNavigationBarTitle({title:'云仓'})
	}
  }
  componentWillMount () {
	this.dobt()
  	var _this = this
    Taro.getSystemInfo({
      success:function(res){
        _this.setState({
          pageheight: res.windowHeight
        })
        _this.getdate(0)
      }
      
    })
    this.gettj()
    wx.showShareMenu({
	    withShareTicket: true,
	    menus: ['shareAppMessage']
	  });
  }
  componentDidShow () {
		
  }
  componentDidUpdate () {

  }
  gettj(){
  	var url='/yunwarehouse/papi/v1.9/ticket/getTicket/count'
  	var _this=this
		CommonJs.ajaxRequest(url,'','GET',function(res){
      _this.setState({
        tjinfo: res.data
      })
    },'wfw')
  }
	getdate(pageNumber){
		var url='/yunwarehouse/papi/v1.9/user-purchased-shop'
		var _this=this
		var dates={
  		size:10,
  		page:pageNumber
  	}
		if(this.state.appmark!='xjzb'){
			dates['typeId.equals']=1
		}
    CommonJs.ajaxRequest(url,dates,'GET',function(res){
    	if (res) {

  			if(res.data.totalPages>pageNumber){
  		  	var hasNextPage = true;
  		  }
  			else{
  				var hasNextPage = false;
  			}
	      var result = res.data.list
	      var results = _this.state.result
	      for(var i = 0; i < result.length; i++){
	        results.push(result[i]);
	      }
	      if (hasNextPage) {
	        _this.setState({
	          result: results,
	          znwns: '暂无数据',
	          pageNumber: res.data.page,
	          hasNextPage: hasNextPage,
	          scrollPd: true
	        })
	      }
	      else {
	        _this.setState({
	          result: results,
	          znwns: '暂无数据',
	          pageNumber: res.data.page,
	          hasNextPage: hasNextPage,
	          scrollPd: false
	        })
	      }
	    } else {
	      _this.setState({
	        znwns: '暂无数据'
	      })
	    }
  	},"wfw")
	}
	loadMore(){
	
    var _this = this
    var pd = _this.state.scrollPd
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1
      _this.getdate(pageNumber)
    }else{
      var hasNextPage =  _this.state.hasNextPage
      if(hasNextPage){
        Taro.showToast({
          title:'正在加载数据...',
          icon: 'none',
          duration: 2000
        })
      }else{
        Taro.showToast({
          title:'没有更多数据啦！',
          icon: 'none',
          duration: 2000
        })
      }
    }
  }
	open(url){
    CommonJs.openTo(url);
  }
  opento(curl,id,sid,did){

    Taro.setStorage({key:'shopId',data:sid})
		Taro.setStorage({key:'dealerId',data:did})
		var url='/clusterShop/getClusterShopByShopId?shopId='+sid
  	var _this=this;
		CommonJs.ajaxRequest(url,'','GET',function(res){
			var dateinfo = Taro.getStorageSync('dateinfo')
			if(!dateinfo){
				dateinfo={}
			}
			var qid=''
			if(res.content){
				if(res.content.clusterId){
					qid=res.content.clusterId
				}
			}
			dateinfo.qid=qid
			Taro.setStorage({key:'dateinfo',data:dateinfo})
			if(_this.state.appmark=='xjzb'){
				_this.$preload({
		      type:id,
		      malltype:'UC'
		   }) 
			}
			else{
	  		_this.$preload({
		      type:id
		    })
	    }
	    CommonJs.openTo(curl);
		}) 
  }
	getnav(shopid,dealerId){
		var obj={
			type:1
		}
		this.$preload({
	      type:obj
	    })
    Taro.setStorage({key:'shopId',data:shopid})
		Taro.setStorage({key:'dealerId',data:dealerId})
		var url='/clusterShop/getClusterShopByShopId?shopId='+shopid
    var _this=this;
    CommonJs.ajaxRequest(url,'','GET',function(res){
    	//console.log(JSON.stringify(res))
    	var dateinfo=Taro.getStorageSync('dateinfo');
    	dateinfo.qid=res.content.clusterId
    	Taro.setStorage({key:'dateinfo',data:dateinfo})
	  	_this.open('/pages/mall/shopindex')
	  })
  }
  render () {
  	const result = this.state.result
	  const tjinfo = this.state.tjinfo
	  const appmark = this.state.appmark
    return (
    	<View className='wgmpage footerpage gxcpage'>
    		<ScrollView className='qun-goods' scrollY='true' style={'height:'+this.state.pageheight+'px'} onScrollToLower={this.loadMore.bind(this,'')}>
    		
    			
    			<View className='gxc-shop'>
    				
          <View className='nodate' style={!result||result.length==0?'':'display:none'}>还没有采购过哟~~</View>
    				{result.map((item, index) => (
						<View className='gxc-list'>
    				<View className='zhibo-dl ' onClick={this.getnav.bind(this,item.shopId,item.dealerId)}>
	       			<View className='zhibo-dt'>
	       				<Image className='qun-userg' mode='aspectFill' src={item.shopVM.rotationPictures?item.shopVM.rotationPictures:'https://minichat.g2ma.com/kedouimg/xjlogo.png'}></Image>
	       			</View>
	       			<View className='zhibo-dd'>
	       				<View className='zhibo-tit'>{item.shopVM.name}</View>
	       				<View className='zhibo-time'>{CommonJs.FormatDate(item.createAt)}</View>
	       			</View>
	       			</View>
					   <View className='zhibo-img'>
					   		{item.itemSelfVOList.map((items, indexs) => (
                			<View className='zhibo-imgs' onClick={this.opento.bind(this,'/pages/mall/goodsxq', items.id,item.shopId,item.dealerId)} style={indexs > 2? 'display:none':''}><Image className='shopimg' src={items.logoUrl}></Image></View>
                			))}
             			 </View>
	       		
				   </View>
	       		))}
    			</View>
				</ScrollView>
      </View>
    )
  }
}
